﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KenLib.Common;
using KenLib.BusinessManager;
using DataInfo;
using System.Collections.Generic;

public partial class MasterPage : System.Web.UI.MasterPage
{
    public string txt = "";
    本社情報Manager _本社情報Manager = new 本社情報Manager();
    基本情報Manager _基本情報Manager = new 基本情報Manager();
    /// <summary>
    /// Coder: Dong
    /// Noi dung: Hien thi ten va quyen dang nhap len master pager
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["frmD_Login_user"] != null && Session["frmD_Login_user"].ToString() != "")
        {
            if (Session["frmD_Login_user"].ToString() == "4")
            {
                if (Session["frmD_Login_企業コード"] != null && Session["frmD_Login_企業コード"].ToString() != "")
                {
                    DataTable dt1 = new DataTable();
                    dt1 = _本社情報Manager.frmD_UserLoginManager_Select企業ID_本社情報(Session["frmD_Login_企業コード"].ToString());
                    if (dt1.Rows.Count > 0)
                    {
                        lbl_User.Text = ShowTextLength(dt1.Rows[0]["企業名"].ToString());
                        lbl_User.ToolTip = dt1.Rows[0]["企業名"].ToString();
                    }
                    else
                    {
                        lbl_User.Text = "";
                    }
                }
                else
                {
                    Response.Redirect("frmD_Login.aspx");
                }
            }
            else if (Session["frmD_Login_user"].ToString() == "5")
            {
                lbl_User.Text = "Administrator";
                lbl_User.ForeColor = System.Drawing.Color.Yellow;
            }
            else if (Session["frmD_Login_user"].ToString() == "6")
            {
                DataTable dt1 = new DataTable();
                DataTable dt2 = new DataTable();
                if (Session["frmD_Login_企業コード"] != null && Session["frmD_Login_企業コード"].ToString() != "" && Session["frmD_Login_人コード"] != null && Session["frmD_Login_人コード"].ToString() != "")
                {
                    dt1 = _本社情報Manager.frmD_UserLoginManager_Select企業ID_本社情報(Session["frmD_Login_企業コード"].ToString());
                    dt2 = _基本情報Manager.frmD_Login_Get社員ID(Convert.ToInt32(dt1.Rows[0]["企業ID"]), Session["frmD_Login_人コード"].ToString());
                    if (dt1.Rows.Count > 0 && dt2.Rows.Count > 0)
                    {
                        lbl_User.Text = ShowTextLength(dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString());
                        lbl_User.ToolTip = dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString();
                        lbl_User.ForeColor = System.Drawing.Color.Yellow;
                    }
                    else
                    {
                        Response.Redirect("frmD_Login.aspx");
                    }
                }
            }
            else if (Session["frmD_Login_user"].ToString() == "7")
            {
                DataTable dt1 = new DataTable();
                DataTable dt2 = new DataTable();
                if (Session["frmD_Login_企業コード"] != null && Session["frmD_Login_企業コード"].ToString() != "" && Session["frmD_Login_人コード"] != null && Session["frmD_Login_人コード"].ToString() != "")
                {
                    dt1 = _本社情報Manager.frmD_UserLoginManager_Select企業ID_本社情報(Session["frmD_Login_企業コード"].ToString());
                    dt2 = _基本情報Manager.frmD_Login_Get社員ID(Convert.ToInt32(dt1.Rows[0]["企業ID"]), Session["frmD_Login_人コード"].ToString());
                    if (dt1.Rows.Count > 0 && dt2.Rows.Count > 0)
                    {
                        lbl_User.Text =ShowTextLength(dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString());
                        lbl_User.ToolTip = dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString();
                    }
                    else
                    {
                        Response.Redirect("frmD_Login.aspx");
                    }
                }
            }
            else if (Session["frmD_Login_user"].ToString() == "8")
            {
                DataTable dt1 = new DataTable();
                DataTable dt2 = new DataTable();
                if (Session["frmD_Login_企業コード"] != null && Session["frmD_Login_企業コード"].ToString() != "" && Session["frmD_Login_人コード"] != null && Session["frmD_Login_人コード"].ToString() != "")
                {
                    dt1 = _本社情報Manager.frmD_UserLoginManager_Select企業ID_本社情報(Session["frmD_Login_企業コード"].ToString());
                    dt2 = _基本情報Manager.frmD_Login_Get社員ID(Convert.ToInt32(dt1.Rows[0]["企業ID"]), Session["frmD_Login_人コード"].ToString());
                    if (dt1.Rows.Count > 0 && dt2.Rows.Count > 0)
                    {
                        lbl_User.Text = ShowTextLength( dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString());
                        lbl_User.ToolTip = dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString();
                        lbl_User.ForeColor = System.Drawing.Color.Yellow;
                    }
                    else
                    {
                        Response.Redirect("frmD_Login.aspx");
                    }
                }
            }
            else if (Session["frmD_Login_user"].ToString() == "9")
            {
                DataTable dt1 = new DataTable();
                DataTable dt2 = new DataTable();
                if (Session["frmD_Login_企業コード"] != null && Session["frmD_Login_企業コード"].ToString() != "" && Session["frmD_Login_人コード"] != null && Session["frmD_Login_人コード"].ToString() != "")
                {
                    dt1 = _本社情報Manager.frmD_UserLoginManager_Select企業ID_本社情報(Session["frmD_Login_企業コード"].ToString());
                    dt2 = _基本情報Manager.frmD_Login_Get社員ID(Convert.ToInt32(dt1.Rows[0]["企業ID"]), Session["frmD_Login_人コード"].ToString());
                    if (dt1.Rows.Count > 0 && dt2.Rows.Count > 0)
                    {
                        lbl_User.Text = ShowTextLength( dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString());
                        lbl_User.ToolTip = dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString();
                    }
                    else
                    {
                        Response.Redirect("frmD_Login.aspx");
                    }
                }
            }
            else
            {
                Response.Redirect("frmD_Login.aspx");
            }
        }
        else
        {
            //Response.Redirect("frmD_Login.aspx");
            lbl_User.Text = "";
        }
        
      
        //if (!IsPostBack)
        //{
        HideLiArea();

        //}

    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Cut string when string too long
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ShowTextLength(string temp)
    {

        if (checkContainsUnicodeCharacter(temp))
        {
            //true tieng nhat
            if (temp.Length > 50)
            {
                temp = temp.Substring(0, 50) + "...";
            }
        }
        else
        {
            //tieng viet
            if (temp.Length > 80)
            {
                temp = temp.Substring(0, 80) + "...";
            }
        }

        return temp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Check byte string
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public bool checkContainsUnicodeCharacter(string input)
    {
        const int MaxAnsiCode = 255;
        char[] a = input.ToCharArray();
        for (int i = 0; i < a.Length; i++)
        {
            if (a[i] > MaxAnsiCode)
            {
                return true;
            }
        }
        return false;
    }

    public void changeTitle(string value)
    {
        lbl_titleHeader.Text = value;
    }
    public void setVisibleLogout(bool visible)
    {
        lbtnLogout.Visible = visible;
    }
    public void HideLiArea()
    {
        if (SessionRepository.Instance.LoginId.Equals(0))
        {
            if (SessionRepository.Instance.CMND.Length > 0)
            {
                List<学歴情報> lst = new GlobalManager().GetInfoInTableByValue<学歴情報>("学歴情報", "社員ID", SessionRepository.Instance.CMND, false);
                List<会社履歴情報> lst1 = new GlobalManager().GetInfoInTableByValue<会社履歴情報>("会社履歴情報", "社員ID", SessionRepository.Instance.CMND, false);
                if (lst.Count == 0 || lst1.Count == 0)
                {
                    //////tagLiCV.Disabled = false;
                    //tagLiCV_a.HRef = "frm_TraineeAcademicInfo.aspx";
                    //tagLiAD.Disabled = true;
                    //tagLiAD_a.HRef = "";
                }
                else
                {
                    ////tagLiCV.Disabled = true;
                    //tagLiCV_a.HRef = "";
                    List<家族情報> lst2 = new GlobalManager().GetInfoInTableByValue<家族情報>("家族情報", "社員ID", SessionRepository.Instance.CMND, false);
                    List<本人住所情報> lst3 = new GlobalManager().GetInfoInTableByValue<本人住所情報>("本人住所情報", "社員ID", SessionRepository.Instance.CMND, false);
                    if (lst2.Count == 0 || lst3.Count == 0)
                    {
                        //tagLiAD.Disabled = false;
                        //tagLiAD_a.HRef = "frm_TraineeFamilyInfo.aspx";
                    }
                    else
                    {
                        //tagLiAD.Disabled = true;
                        //tagLiAD_a.HRef = "";
                        txt = "Bạn đã nhập đầy đủ các thông tin.";
                    }
                }
                //tagLiRegister.Disabled = true;
                //tagLiRegister_a.HRef = "";
                lbtnLogout.Visible = true;
                lbl_NameHeader.Text = AppCode_Common.GetUserName(SessionRepository.Instance.CMND);
            }
            else
            {
                //tagLiRegister.Disabled = false;
                //tagLiRegister_a.HRef = "frm_TraineeBasicInfo.aspx";
                ////tagLiCV.Disabled = true;
                //tagLiCV_a.HRef="";
                //tagLiAD.Disabled = true;
                //tagLiAD_a.HRef = "";
                //lbtnLogout.Visible = true;
            }
            //tagLiCICS.Visible = false;
            //tagLiKansai.Visible = false;
            //tagLiComp.Visible = false;
        }
        if (SessionRepository.Instance.LoginId.Equals(1))
        {
            //tagLiTrainee.Visible = false;
            //tagLiCICS.Visible = false;
            //tagLiKansai.Visible = false;
            //tagLiComp.Visible = true;
        }
        if (SessionRepository.Instance.LoginId.Equals(4))
        {
            //tagLiTrainee.Visible = false;
        }

    }

    protected void lbtnLogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("~/frmD_Login.aspx");
    }


}
